﻿Ext.define("PSI.Home.MainPage", {

    createUI: function () {
        var me = this;
        var panel = Ext.create("Ext.panel.Panel", {
            border: 0,
            layout: "border",
            items: [{
                region: "north",
                height: "100%",
                title: '首页',
                layout: "hbox",
                bodyPadding: "20 30 20 30",
                border: false,
                items: [
                   {
                       xtype: "panel",
                       title: '通知',
                     //  draggable: true,      //可拖拽的 
                       collapsible: true,    //可折叠的
                       //header:false,        //隐藏标题
                       margin: "0 20 0 0",
                       height: "50%",
                       width: "50%",
                       layout:"fit",
                       items: [
                           me.getReplyItem()
                       ]

                   }, {
                       xtype: "panel",
                       title: '知识管理',
                       //  draggable: true,      //可拖拽的 
                       collapsible: true,    //可折叠的
                       //header:false,        //隐藏标题
                       margin: "0 0 0 20",
                       height: "50%",
                       width: "50%",
                       layout: "fit",
                       items: [
                          me.KnowledgeItem()
                       ]
                   }
                ]
            }
                //, {
                //    region: "south",
                //    height: "50%",
                //    width: "50%",
                //    layout: "fit",
                //    bodyPadding: "20 30 20 30",
                //    border: false
                //}
            ]
        });

        me.panel = panel;
    },

    getMainPanel: function () {
        return this.panel;
    },

    getReplyItem: function () {
        var me = this;
        var emodel = Ext.define("moldes", {
            extend: "Ext.data.Model",
            fields: ["Id", "ExpriatinDate", "Title" ]
        })
        var store = Ext.create("Ext.data.Store", {
            model: emodel,
            storeId: 'kRreply',
            proxy: {
                type: 'ajax',
                url: '/Massage/MinaMassageItems',
                extraParams: {},
                actionMethods: { read: 'POST' },
                reader: {
                    type: 'json',
                    root: 'Data'
                }
            },
            autoLoad: true,
            listeners: {
                load: function (me, records, successful, eOpts) {
                }
            }
        })

        return Ext.create("Ext.grid.Panel", {
            viewConfig: { enableTextSelection: true },
            columnLines: true,
            border: 0,
            autoScroll:false,
            columns: [

                    { header: "", dataIndex: "id",hidden:true, menuDisabled: true },
                    { header: "标题", dataIndex: "Title", menuDisabled: true,width:"89%" },
                    {
                        header: "截止日期", dataIndex: "ExpriatinDate", menuDisabled: true,width:96, renderer: function (value) {
                            var dt = eval("new " + value.substr(1, value.length - 2)).toString();
                            return Ext.util.Format.date(dt, "Y-m-d");
                        }
                    }

            ],
            store: store,
            listeners: {
                itemdblclick: function (ss, record, item, index, e, eOpts) {
                  
                     me.MassageInfowindow(record.raw.Id);
                }
            }
        });
        //var resultTpl = Ext.create('Ext.XTemplate',
        //   '<tpl for=".">',
        //   '<div class="search-item">',
        //       '<h3><span>{ExpriatinDate:this.formatDate}</span>',
        //       '<a href="#" name={Id} onclick="me.MassageInfowindow(this.name)">{Title}</a></h3>',
        //   '</div></tpl>',
        //   {
        //       formatDate: function (value) {
        //           var dt = eval("new " + value.substr(1, value.length - 2)).toString();
        //           return Ext.util.Format.date(dt, "Y-m-d");
        //       }
        //   });

        //return Ext.create("Ext.view.View", {
        //    viewConfig: { loadingText: "数据加载中...." },
        //    overflowY: 'auto',
        //    tpl: resultTpl,
        //    store: store,
        //    itemSelector: 'div.search-item',
        //    emptyText: '',
        //    listeners: {
        //        itemclick: function (me, record, item, index, e, eOpts) {

        //        }
        //    }
        //})
    },

    MassageInfowindow: function (id) {
        me = this;

        var cc = Ext.create("Ext.window.Window", {
            modal: true,
            id: 'arcform',
            title: '发表回复',
            width: 700,
            height: 230,
            layout: "fit",
            items: [
                 {
                     xtype: "form",
                     id: "editform",
                     region: "south",
                     border: 0,
                     layout: 'fit',
                     items: [{
                         xtype: 'hidden',
                         name: 'KthemeID',
                         id: 'kthemeid',
                         value: id
                     },
                         {
                             xtype: 'panel',
                             id: "contents",
                             name: "contens",
                             value: "",
                             dockedItems: [{
                                 xtype: 'toolbar',
                                 dock: 'bottom',
                                 height: 30,
                                 items: [
                                     { xtype: 'label', text: '下载附件:', style: { fontSize: "12px", color: "red" } },
                                     {
                                         xtype: 'button',
                                         text: '附件1.xls',
                                         id: "f1",

                                         margin: '0 0 0 30',
                                         listeners: {
                                             click: function (me, v) {
                                                 tree = Ext.getCmp("kthemeid");
                                                 themeid = tree.getValue();
                                                 window.location.href = "/Massage/DownLoad?num=1&id=" + themeid
                                             }

                                         }

                                     }, {
                                         xtype: 'button',
                                         text: '附件2.xls',
                                         id: "f2",

                                         margin: '0 0 0 30',
                                         listeners: {
                                             click: function (me, v) {
                                                 tree = Ext.getCmp("kthemeid");
                                                 themeid = tree.getValue();
                                                 window.location.href = "/Massage/DownLoad?num=2&id=" + themeid
                                             }

                                         }

                                     }
                                 ]
                             }]
                         }
                     ]

                 }
            ]
        }).show();
        Ext.Ajax.request({
            url: "/Massage/Getsingle",
            method: "POST",
            params: { id: id },
            callback: function (options, success, response) {
                if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    Ext.getCmp("arcform").setTitle(data.Data.Title);
                    Ext.getCmp("contents").body.update(data.Data.Contents);
                    if (data.Data.File1 != null && data.Data.File1 != "") {
                        Ext.getCmp("f1").setVisible(true);
                        var f1 = data.Data.File1.split("_");
                        Ext.getCmp("f1").setText(f1[1]);
                    }
                    else {
                        Ext.getCmp("f1").setText("");
                        Ext.getCmp("f1").setVisible(false);
                    }

                    if (data.Data.File2 != null && data.Data.File2 != "") {
                        Ext.getCmp("f2").setVisible(true);
                        var f2 = data.Data.File2.split("_");
                        Ext.getCmp("f2").setText(f2[1]);
                    }
                    else {
                        Ext.getCmp("f2").setText("");
                        Ext.getCmp("f2").setVisible(false);
                    }
                } else {
                    PSI.MsgBox.showInfo("网络错误");
                }
            }
        });


        me.replywin = cc
        return cc;
    },

    KnowledgeItem: function ()
    {
        var me = this;
        var emodel = Ext.define("moldes", {
            extend: "Ext.data.Model",
            fields: ["id", "Contents", "title" ]
        })
        var store = Ext.create("Ext.data.Store", {
            model: emodel,
            storeId: 'knowstore',
            proxy: {
                type: 'ajax',
                url: '/Knowledge/MainPage',
                extraParams: {},
                actionMethods: { read: 'Get' },
                reader: {
                    type: 'json',
                    root: 'Data'
                }
            },
            autoLoad: true,
            listeners: {
                load: function (me, records, successful, eOpts) {

                }
            }
        })

        return Ext.create("Ext.grid.Panel", {
            viewConfig: { enableTextSelection: true },
            columnLines: true,
            border: 0,
            autoScroll: false,
            columns: [

                    { header: "", dataIndex: "id", hidden: true, menuDisabled: true },
                    { header: "标题", dataIndex: "title", menuDisabled: true, width: "80%" },
                    {
                        header: "类别", dataIndex: "Contents", menuDisabled: true,width:180
                    }

            ],
            store: store,
            listeners: {
                itemdblclick: function (ss, record, item, index, e, eOpts) {
                    window.location.href = "Knowledge/Index?id=" + record.raw.id;
                }
            }
        });


    }

});


